package com.vogue.supplymall.order.controller;

import com.vogue.supplymall.common.domain.dto.ControllerResultInfo;
import com.vogue.supplymall.common.domain.dto.OrderDetailInfo;
import com.vogue.supplymall.common.domain.dto.UploadToDBInfo;
import com.vogue.supplymall.common.domain.dto.UserInfo;
import com.vogue.supplymall.common.domain.tool.MessageSourceUtils;
import com.vogue.supplymall.common.exception.DataNotFindException;
import com.vogue.supplymall.common.exception.UploadException;
import com.vogue.supplymall.order.common.CheckOrderUtils;
import com.vogue.supplymall.order.domain.biz.OrderDetailBiz;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * The type Order detail controller.
 *
 * @aouthor: zhangliang
 * @date: Create in 2018-02-06
 */
@RestController
public class OrderDetailController extends BaseController {
    /**
     * The constant log.
     */
    public static final Logger log = LoggerFactory.getLogger(OrderDetailController.class);

    @Resource
    private OrderDetailBiz orderDetailBiz;
    @Resource
    private MessageSourceUtils messageSourceUtils;

    /**
     * Gets order detail.
     * 订单详情
     *
     * @param appver          the appver
     * @param orderDetailInfo the order detail info
     * @return the order detail
     */
    @RequestMapping(value = "/is/{appver}/order/detail", method = RequestMethod.POST)
    public ResponseEntity<Object> getOrderDetail(@PathVariable("appver") String appver, @RequestBody OrderDetailInfo orderDetailInfo) {
        log.info("Get Order Detail start >>>>>> ");
        UserInfo info = new UserInfo();
        if (SecurityUtils.getSubject().isAuthenticated()) {
            info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            log.info("获取登信息：" + info.getLoginId());
            if (CheckOrderUtils.checkOrderDetailParameter(orderDetailInfo)) {
                log.info(" 参数错误！ ");
                throw new DataNotFindException("parameter error");
            }
        } else {
            log.info("获取登信息失败！");
        }

        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(true);
        resultInfo.setData(orderDetailBiz.findOrderDetail(orderDetailInfo, info));
        log.info(" <<<<<< Get Order Detail end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }


    /**
     * Update order fulfillment response entity.
     * 更新订单物流信息
     *
     * @param appver          the appver
     * @param orderDetailInfo the order detail info
     * @return the response entity
     */
    @RequestMapping(value = "/is/{appver}/order/update/express", method = RequestMethod.POST)
    public ResponseEntity<Object> updateOrderFulfillment(@PathVariable("appver") String appver, @RequestBody OrderDetailInfo orderDetailInfo) {
        log.info("update Order express start >>>>>> ");
        if (SecurityUtils.getSubject().isAuthenticated()) {
            UserInfo info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            log.info("获取登信息：" + info.getLoginId());
            if (null == orderDetailInfo.getExpress() || null == orderDetailInfo.getOrderid()) {
                log.info(" 参数错误！ ");
                throw new DataNotFindException("parameter error");
            }
        } else {
            log.info("获取登信息失败！");
        }

        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(true);
        resultInfo.setData(orderDetailBiz.updateFulfillment(orderDetailInfo));
        /* 返回订单修改成功 */
        resultInfo.setMsg(messageSourceUtils.getMessage("2612", new String[]{}));
        log.info(" <<<<<< update Order express end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    /**
     * Update pay voucher response entity.
     * 更新订单支付凭证（线下）
     *
     * @param appver         the appver
     * @param uploadToDBInfo the upload to db info
     * @return the response entity
     */
    @RequestMapping(value = "/is/{appver}/order/detail/payvoucher/update", method = RequestMethod.POST)
    public ResponseEntity<Object> updatePayVoucher(@PathVariable("appver") String appver, @RequestBody UploadToDBInfo uploadToDBInfo) {
        log.info("update pay voucher start >>>>>> ");
        if (SecurityUtils.getSubject().isAuthenticated()) {
            UserInfo info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            log.info("获取登信息：" + info.getLoginId());
            if (null == uploadToDBInfo.getMainId() ||
                    null == uploadToDBInfo.getSubId() ||
                    null == uploadToDBInfo.getUrl()) {
                log.info(" 参数错误！ ");
                throw new DataNotFindException("parameter error");
            }
        } else {
            log.info("获取登信息失败！");
        }

        try {
            orderDetailBiz.updatePayVoucher(uploadToDBInfo);
        } catch (RuntimeException e){
            throw new UploadException("");
        }
        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(true);
        resultInfo.setData(null);
        /* 返回订单修改成功 */
        resultInfo.setMsg(messageSourceUtils.getMessage("2612", new String[]{}));
        log.info(" <<<<<< update  pay voucher end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }


    /**
     * Gets order detail pay voucher.
     * 获取订单支付凭证
     *
     * @param appver         the appver
     * @param uploadToDBInfo the upload to db info
     * @return the order detail pay voucher
     */
    @RequestMapping(value = "/is/{appver}/order/detail/payvoucher/get", method = RequestMethod.POST)
    public ResponseEntity<Object> getOrderDetailPayVoucher(@PathVariable("appver") String appver, @RequestBody UploadToDBInfo uploadToDBInfo) {
        log.info("Get Order Detail PayVoucher start >>>>>> ");
        UserInfo info = new UserInfo();
        if (SecurityUtils.getSubject().isAuthenticated()) {
            info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            log.info("获取登信息：" + info.getLoginId());
            if (null == uploadToDBInfo.getMainId() || null == uploadToDBInfo.getSubId()) {
                log.info(" 参数错误！ ");
                throw new DataNotFindException("parameter error");
            }
        } else {
            log.info("获取登信息失败！");
        }

        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(true);
        resultInfo.setData(orderDetailBiz.getOrderDetailPayVoucher(uploadToDBInfo));
        log.info(" <<<<<< Get Order Detail PayVoucher end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }
}
